package models

import (
	"time"
	"github.com/astaxie/beego/orm"
	"fmt"
	"strconv"
	"read.com/admin/libs"
	"strings"
)

type ReportDailyBook struct {
	ReportDate time.Time `orm:"column(report_date);type(date)" description:"日报时间"`
	BookId     int       `orm:"column(book_id);auto" description:"小说ID"`
	ReadCount  int64     `orm:"column(read_count)" description:"阅读次数"`
	ReadUsers  int64     `orm:"column(read_users)" description:"阅读人数"`
	BuyCount   int64     `orm:"column(buy_count)" description:"购买次数"`
	BuyUsers   int64     `orm:"column(buy_users)" description:"购买人数"`
}

type FormatReportDailyBook struct {
	ReportDailyBook
	Book
}

func (t *ReportDailyBook) TableName() string {
	return "report_daily_book"
}

func init() {
	orm.RegisterModel(new(ReportDailyBook))
}

func GetReportDailyBookList(page, pageSize int,channel,dateTime,export string) ([]map[string]string,int64,map[int]map[string]string) {
	o := orm.NewOrm()
	offset := (page - 1) * pageSize
	formatReportDailyBooks := make([]*FormatReportDailyBook, 0)
	conChannel := ""
	conDateTime := ""
	if channel != "-1" {
		conChannel = " and b.channel_type = " + channel + " "
	}
	if dateTime != "" {
		conDateTime = " and rb.report_date = '" + dateTime + "' "
	}
	count := 0
	if export == "1" {
		// 一定要先选择日期
		sqlStr := "select b.*,rb.* from book b left join report_daily_book rb on (rb.book_id = b.id " + conDateTime + ") where 1 = 1 " + conChannel +"order by report_date desc limit " + strconv.Itoa(offset) + "," + strconv.Itoa(pageSize)
		_, err := o.Raw(sqlStr).QueryRows(&formatReportDailyBooks)
		if err != nil {
			fmt.Println("select report_daily_book error", err)
		}
	}else{
		sqlStr := "select b.*,rb.* from report_daily_book rb inner join book b on rb.book_id = b.id where 1 = 1 " + conChannel + conDateTime + "order by report_date desc limit " + strconv.Itoa(offset) + "," + strconv.Itoa(pageSize)
		_, err := o.Raw(sqlStr).QueryRows(&formatReportDailyBooks)
		if err != nil {
			fmt.Println("select report_daily_book error", err)
		}
	}

	sqlCount := "select count(*) from report_daily_book rb inner join book b on rb.book_id = b.id where 1 = 1 " + conChannel + conDateTime
	o.Raw(sqlCount).QueryRow(&count)
	results := make([]map[string]string, 0)
	for k, v := range formatReportDailyBooks {
		status := ""
		// 判断书籍是否上架连载完结
		if compare := libs.CompareTime(v.ReleaseTime, time.Now()); compare >= 0 {
			status = "未上架"
		} else {
			if v.Status == 0 {
				status = "更新中"
			} else {
				if v.ReleaseChapterTotal() == v.ChapterTotal {
					status = "已完结"
				} else {
					status = "更新中"
				}
			}
		}
		bookId := v.BookId
		if v.BookId == 0 {
			bookId = v.Id
		}
		result := map[string]string{
			"sort":strconv.Itoa(k+1),
			"reportDate":v.ReportDate.Format("2006-01-02"),
			"bookId":strconv.Itoa(bookId),
			"bookName":v.Name,
			"channel":strconv.Itoa(int(v.ChannelType)),
			"chapterPriceSum":strconv.FormatInt(v.ChapterPriceSum,10),
			"price":strconv.Itoa(v.Price),
			"status":status,
			"chapterTotal":strconv.Itoa(v.ChapterTotal),
			"onlineChapter":strconv.Itoa(v.ReleaseChapterTotal()),
			"chapterStart":strconv.Itoa(v.ChapterStart),
			"chapterDaily":strconv.Itoa(int(v.ChapterDaily)),
			"releaseTime":strings.Split(v.ReleaseTime.Format("2006-01-02 15:04:05"), " ")[0] + " " + v.ChapterTime.Format("15:04:05"),
			"buyUsers":strconv.FormatInt(v.BuyUsers,10),
			"buyCount":strconv.FormatInt(v.BuyCount,10),
			"readUsers":strconv.FormatInt(v.ReadUsers,10),
			"readCount":strconv.FormatInt(v.ReadCount,10),
		}
		results = append(results, result)
	}
	tableHead := map[int]map[string]string{
		0:{"sort":"序号"},
		1:{"reportDate":"日报日期"},
		2:{"bookId":"书Id"},
		3:{"bookName":"书名"},
		4:{"channel":"频道"},
		5:{"chapterPriceSum":"总价"},
		6:{"price":"单价"},
		7:{"status":"状态"},
		8:{"chapterTotal":"章节总数"},
		9:{"onlineChapter":"已更章节"},
		10:{"chapterStart":"始更章节"},
		11:{"chapterDaily":"日更章节"},
		12:{"releaseTime":"上架日期"},
		13:{"buyUsers":"购买人数"},
		14:{"buyCount":"购买章节数"},
		15:{"readUsers":"阅读人数"},
		16:{"readCount":"阅读章节数"},
	}
	return results,int64(count),tableHead
}
